/*
 * @Author: hx 2542123711@QQ.com
 * @Date: 2024-12-17 18:04:48
 * @LastEditors: hx 2542123711@QQ.com
 * @LastEditTime: 2024-12-17 18:50:04
 * @FilePath: /class_thread/Thread/Thread.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEpth
 */
#pragma once

#include<iostream>
#include<string>
#include<pthread.h>
#include<ctime>

using namespace std;


typedef void(*callback)();
//typedef void(*callback_int)(int);

template<class T>
class Thread
{
public:
    Thread(callback cb, T data)
    :_tid(0), _name(""), _time(0), _isrunning(false), _cb(cb) /*_data(data)*/
    {}

    void Entery()
    {
        _cb();
    }

    static void* Routine(void* args)
    {
        Thread* t = static_cast<Thread*>(args);
        t->Entery();
        //t->_cb();
        return nullptr;
    }

    void Run()
    {
        _name = "thread";
        _time = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, Routine, this);
    }

    void Join()
    {
        pthread_join(_tid, nullptr);
    }

    string GetThreadName()
    {
        return _name;
    }

    uint64_t GetTime()
    {
        return _time;
    }

    bool IsRunning()
    {
        return _isrunning;
        _isrunning = false;
    }

    ~Thread()
    {}
private:
    pthread_t _tid;
    string _name;
    uint64_t _time;
    bool _isrunning;

    callback _cb;
    //T _data;
};
